package io.renren.modules.job.task;

import io.renren.common.utils.DateUtils;
import io.renren.midplat.fuseclick.task.BatchSetOffers;
import io.renren.midplat.offerslook.affiliate.PullOffersByOffersLookServiceImpl;
import io.renren.modules.advertiser.entity.AdvertiserEntity;
import io.renren.modules.advertiser.service.AdvertiserService;
import io.renren.modules.offer.service.OfferService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * offerslook 定时拉取任务
 *
 * @Description /aff/v1/batches/offers
 * @Author haijun.zhang@luckincoffee.com
 * @Date 2018-08-07 9:45
 */
@Component("offersLookToFuseClickTask")
public class OffersLookToFuseClickTask {

    @Autowired
    private AdvertiserService advertiserService;

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OffersLookToFuseClickTask.class);

    @Autowired
    @Qualifier("pullOffersByOffersLookService")
    private PullOffersByOffersLookServiceImpl pullOffersByOffersLookService;

    @Async
    public void taskRun() {
        String batchNo = "MA-"+DateUtils.get12Time();
        Thread.currentThread().setName(batchNo);
        LOGGER.info("offersLookToFuseClickTask start ------------"+batchNo);
        List<AdvertiserEntity> advertiserEntities =  advertiserService.selectByMap(new HashMap<String, Object>());
        LOGGER.info("OffersLook广告主的数量为："+advertiserEntities.size());
        for (final AdvertiserEntity advertiserEntity : advertiserEntities) {
            if("Active".equals(advertiserEntity.getStatus()) || "Testing".equals(advertiserEntity.getStatus())){
                LOGGER.info("广告主："+advertiserEntity.getSspChannelId()+"准备异步跑批，状态："+advertiserEntity.getStatus());
                pullOffersByOffersLookService.serviceAsync(advertiserEntity);
            }else {
                LOGGER.error("广告主："+advertiserEntity.getSspChannelId()+"不会跑批，状态："+advertiserEntity.getStatus());
            }
        }
        LOGGER.info("offersLookToFuseClickTask end ------------"+batchNo);
    }


}
